package com.exm.collectcodenew.juc.copyonWrite;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
//多线程下法代器的弱一致性
public class CopyOnWriteArrayListTest {
    public static void main(String[] args) throws InterruptedException {
        CopyOnWriteArrayList<String> arrayList =new CopyOnWriteArrayList<> ();
        arrayList.add ( "hello" );
        arrayList.add ( "alibaba" );
        arrayList.add ( "welcome" );
        arrayList.add ( "to");
        arrayList.add ( "hangzhou") ;
        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                //修改list 中下标为1 的元素为baba
                arrayList.set(1, "baba");
                //删除元素
                arrayList.remove(2);
                arrayList.remove(3);
            }
        }) ;
        //保证在修改线程启动前获取迭代器
            Iterator<String> itr = arrayList.iterator() ;
        // 启动线程
        threadOne.start() ;
        //等待子线程执行完毕
        threadOne.join() ;
        //迭代元素
        while ( itr.hasNext () ) {
            System.out.println (itr.next()) ;
        }
    }
}
